package me.hyman.testcustomview.viewDragHelper.drawer;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import me.hyman.testcustomview.R;

public class LeftMenuFragment extends ListFragment {

    private LayoutInflater layoutInflater;
    private LeftMenuAdapter leftMenuAdapter;

    private MenuItem[] items = new MenuItem[3];


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        layoutInflater = LayoutInflater.from(getActivity());

        for(int i=0; i<3; i++) {
            items[i] = new MenuItem(getResources().getStringArray(R.array.array_left_menu)[i], false, R.mipmap.music_36px, R.mipmap.music_36px_light);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.setBackgroundColor(0xffffffff);

        leftMenuAdapter = new LeftMenuAdapter(getActivity(), items);
        setListAdapter(leftMenuAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if(onMenuItemClickedListener != null) {
            //onMenuItemClickedListener.onMenuItemClicked(items[position].text);
            onMenuItemClickedListener.onMenuItemClicked(((MenuItem)getListAdapter().getItem(position)).text);
        }
        leftMenuAdapter.setSelection(position);
    }

    public interface OnMenuItemClickedListener {
        void onMenuItemClicked(String text);
    }

    private OnMenuItemClickedListener onMenuItemClickedListener;

    public void setOnMenuItemClickedListener(OnMenuItemClickedListener onMenuItemClickedListener) {
        this.onMenuItemClickedListener = onMenuItemClickedListener;
    }
}
